package com.tedu.day08springmvc.controller;

import com.tedu.day08springmvc.mapper.UserMapper;
import com.tedu.day08springmvc.pojo.dto.UserDTO;
import com.tedu.day08springmvc.pojo.entity.User;
import com.tedu.day08springmvc.pojo.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
public class UserController {
    @Autowired
    private UserMapper userMapper;

    //@RequestMapping("/users/reg")
    @PostMapping("/users/reg")
    //@ResponseBody
    public String reg(UserDTO userDTO){
        User user=new User();
        System.out.println(user);
        BeanUtils.copyProperties(userDTO,user);
        user.setCreated(new Date());
        System.out.println(user);
        userMapper.insertUser(user);
        return "注册成功";
    }
    //@RequestMapping("/users/delete")
    @DeleteMapping("/users/delete/{id}")
    //@ResponseBody
    public String delete(@PathVariable int id){
        int num= userMapper.deleteUser(id);
        return num>0?"删除成功":"删除失败";
    }
    //@RequestMapping("/users/update")
    @PutMapping("/users/update")
   // @ResponseBody
    public String update(UserDTO userDTO){
        User user=new User();
        BeanUtils.copyProperties(userDTO,user);
        int num=userMapper.updateUser(user);
        return num>0?"修改成功":"修改失败";
    }

    //@RequestMapping(value = "/users/find" ,method = RequestMethod.GET)
    @GetMapping("/users/find")
   // @ResponseBody
    public List<UserVO> find(){
        List<UserVO> userVOList=userMapper.selectUsers();
        return userVOList;
    }

    //测试查看id为12的用户信息
    public UserVO findById(int id){
        return null;
    }


}
